<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属计划" prop="planId">
        <el-select v-model="queryParams.planId" placeholder="请选择所属计划" clearable>
          <el-option
            v-for="item in planList"
            :key="item.planId"
            :label="item.planName"
            :value="item.planId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属车间" prop="workshopId">
        <el-select v-model="queryParams.workshopId" placeholder="请选择车间" clearable>
          <el-option
            v-for="item in workshopList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属班组" prop="groupId">
        <el-cascader
          v-model="queryParams.groupId"
          :options="this.workshopList"
          :props="{ expandTrigger: 'hover', emitPath:false }" clearable></el-cascader>
      </el-form-item>
<!--      <el-form-item label="任务优先级" prop="taskPriority">-->
<!--        <el-input-number-->
<!--          v-model="queryParams.taskPriority"-->
<!--          placeholder="请输入任务优先级"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--          :min="0" step-strictly-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="开始时间" prop="startTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.startTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择开始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="结束时间" prop="endTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.endTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择结束时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务规模" prop="taskScale">
        <el-select v-model="queryParams.taskScale" placeholder="请选择任务规模" clearable>
          <el-option
            v-for="dict in dict.type.scale"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品" prop="productId">
        <el-select v-model="queryParams.productId" placeholder="请选择产品" clearable>
          <el-option
            v-for="item in productList"
            :key="item.productId"
            :label="item.productName"
            :value="item.productId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aero:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['aero:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['aero:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['aero:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="所属计划" align="center" prop="planInfo.planName" />
      <el-table-column label="任务负责人编号" align="center" prop="staffInfo.staffNo" />
      <el-table-column label="任务负责人姓名" align="center" prop="staffInfo.staffName" />
      <el-table-column label="任务优先级" align="center" prop="taskPriority" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_status" :value="scope.row.taskStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="任务规模" align="center" prop="taskScale">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.scale" :value="scope.row.taskScale"/>
        </template>
      </el-table-column>
      <el-table-column label="产品" align="center" prop="productInfo.productName" />
      <el-table-column label="任务预计产量" align="center" prop="dueNum" />
      <el-table-column label="任务实际产量" align="center" prop="actualNum" />
      <el-table-column label="车间编码" align="center" prop="workshopInfo.workshopNo" />
      <el-table-column label="班组编码" align="center" prop="groupInfo.groupNo" />
      <el-table-column label="分数" align="center" prop="score" width="180">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.score"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aero:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aero:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="所属计划" prop="planId">
          <el-select v-model="form.planId" placeholder="请选择所属计划" clearable>
            <el-option
              v-for="item in planList"
              :key="item.planId"
              :label="item.planName"
              :value="item.planId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="任务优先级" prop="taskPriority">
          <el-input-number v-model="form.taskPriority" placeholder="请输入任务优先级" :min="0" step-strictly/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatus">
          <el-radio-group v-model="form.taskStatus">
            <el-radio
              v-for="dict in dict.type.task_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务规模" prop="taskScale">
          <el-radio-group v-model="form.taskScale">
            <el-radio
              v-for="dict in dict.type.scale"
              :key="dict.value"
              :label="dict.value"
              :disabled="dict.value === Scale().FACTORY_LEVEL"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属车间" prop="workshopId" v-if="form.taskScale === Scale().WORKSHOP_LEVEL">
          <el-select v-model="form.workshopId" placeholder="请选择车间" clearable>
            <el-option
              v-for="item in workshopList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-tag>  若所属计划为车间级，则此处随意选择，后台自动匹配</el-tag>
        </el-form-item>
        <el-form-item label="所属班组" prop="groupId" v-if="form.taskScale === Scale().GROUP_LEVEL">
          <el-cascader
            v-model="form.groupId"
            :options="this.workshopList"
            :props="{ expandTrigger: 'hover', emitPath:false }" clearable></el-cascader>
          <el-tag>  若所属计划为车间级，则必须选择对应车间内的班组，否则无法提交</el-tag>
        </el-form-item>
        <el-form-item label="产品" prop="productId">
          <el-select v-model="form.productId" placeholder="请选择产品" clearable>
            <el-option
              v-for="item in productList"
              :key="item.productId"
              :label="item.productName"
              :value="item.productId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="任务预计产量" prop="dueNum">
          <el-input-number v-model="form.dueNum" placeholder="请输入任务预计产量" :min="1" step-strictly/>
        </el-form-item>
        <el-form-item label="任务实际产量" prop="actualNum">
          <el-input-number v-model="form.actualNum" placeholder="请输入任务实际产量" :min="0" step-strictly/>
        </el-form-item>
        <el-form-item label="分数" prop="score" v-if="form.taskStatus === TaskStatus().FINISHED">
          <el-rate v-model="form.score" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/aero/task";
import { listAllPlan } from "@/api/aero/plan";
import { listAllProduct } from "@/api/aero/product";
import { listStaffByPost } from "@/api/aero/staff";
import { listAllWorkshop } from "@/api/aero/workshop";
import { Scale, TaskStatus } from "@/enums";

export default {
  name: "Task",
  dicts: ['task_status', 'scale'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务信息表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        planId: null,
        taskLeaderId: null,
        taskPriority: null,
        startTime: null,
        endTime: null,
        taskStatus: null,
        taskScale: null,
        productId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        planId: [
          { required: true, message: "所属计划id不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        taskStatus: [
          { required: true, message: "任务状态不能为空", trigger: "change" }
        ],
        taskScale: [
          { required: true, message: "任务规模不能为空", trigger: "change" }
        ],
        productId: [
          { required: true, message: "产品id不能为空", trigger: "blur" }
        ],
        dueNum: [
          { required: true, message: "任务预计产量不能为空", trigger: "blur" }
        ],
        workshopId: [
          { required: true, message: "所属车间不能为空", trigger: "blur" }
        ],
        groupId: [
          { required: true, message: "所属班组不能为空", trigger: "blur" }
        ],
      },
      // 计划信息列表
      planList: [],
      // 产品信息列表
      productList: [],
      // 员工列表
      staffList: [],
      // 车间信息列表
      workshopList: [],
    };
  },
  created() {
    this.getList();
    this.getProductList();
    this.getPlanList();
    this.getStaffList();
    this.getWorkshopList();
  },
  activated() {
    this.getProductList();
    this.getPlanList();
    this.getStaffList();
    this.getWorkshopList();
  },
  methods: {
    Scale() {
      return Scale;
    },
    TaskStatus() {
      return TaskStatus;
    },
    /** 查询任务信息列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        taskName: null,
        planId: null,
        taskLeaderId: null,
        taskPriority: null,
        startTime: null,
        endTime: null,
        taskStatus: null,
        taskScale: null,
        productId: null,
        dueNum: null,
        actualNum: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const taskId = row.taskId || this.ids
      getTask(taskId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskId != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids;
      this.$modal.confirm('是否确认删除任务信息编号为"' + taskIds + '"的数据项？').then(function() {
        return delTask(taskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('aero/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    /** 查询计划信息 */
    getPlanList() {
      listAllPlan(null).then(response => {
        this.planList = response.data;
      })
    },
    /** 查询产品信息 */
    getProductList() {
      listAllProduct(null).then(response => {
        this.productList = response.data;
      })
    },
    /** 查询所有员工 */
    getStaffList() {
      listStaffByPost("all").then(response => {
        this.staffList = response.data;
      })
    },
    /** 获取车间信息列表 */
    getWorkshopList() {
      listAllWorkshop(null).then(response => {
        console.log(response.data);
        let workshopList = response.data;
        if (!workshopList || workshopList.length <= 0) return;

        let temp = [];
        for (let i in workshopList) {
          let workshop = {};
          workshop.label = workshopList[i].workshopNo;
          workshop.value = workshopList[i].workshopId;

          let children = [];
          if (workshopList[i].groupInfoList && workshopList[i].groupInfoList.length > 0) {
            for (let j in workshopList[i].groupInfoList) {
              let groupInfo = {};
              groupInfo.label = workshopList[i].groupInfoList[j].groupNo;
              groupInfo.value = workshopList[i].groupInfoList[j].groupId;
              children.push(groupInfo);
            }
            workshop.children = children;
          }

          temp.push(workshop);
        }
        this.workshopList = temp;
      })
    }
  }
};
</script>
